//
//  RegisterViewController.m
//  SheShiDu
//
//  Created by 泉  姜 on 16/6/22.
//  Copyright © 2016年 单剑秋. All rights reserved.
//

#import "RegisterViewController.h"

@interface RegisterViewController ()<UITextFieldDelegate>

@end

@implementation RegisterViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = [UIColor colorWithRed:234 / 255.0 green:234 / 255.0 blue:234 / 255.0 alpha:1];
    
    [self setView];
    
}

- (void)setView
{
    WEAKSELF(weakSelf);
    
    UIView *backView = [[UIView alloc]init];
    backView.backgroundColor = [UIColor colorWithRed:35 / 255.0 green:195 / 255.0 blue:183 / 255.0 alpha:1];
    backView.layer.masksToBounds = YES;
    backView.layer.cornerRadius = 8;
    [self.view addSubview:backView];
    [backView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(weakSelf.view.mas_top).offset(80);
        make.bottom.mas_equalTo(weakSelf.view.mas_bottom).offset(-80);
        make.left.mas_equalTo(weakSelf.view.mas_left).offset(40);
        make.right.mas_equalTo(weakSelf.view.mas_right).offset(-40);
    }];
    
    UILabel *titleLabel = [[UILabel alloc]init];
    titleLabel.text = @"注册";
    titleLabel.textAlignment = NSTextAlignmentCenter;
    titleLabel.font = [UIFont boldSystemFontOfSize:23];
    titleLabel.textColor = [UIColor whiteColor];
    [backView addSubview:titleLabel];
    [titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(CGSizeMake(70, 40));
        make.top.mas_equalTo(backView.mas_top).offset(5);
        make.centerX.equalTo(backView.mas_centerX);
    }];
    
    self.nameTF = [[UITextField alloc]init];
    _nameTF.delegate = self;
    _nameTF.borderStyle = UITextBorderStyleRoundedRect;
    _nameTF.placeholder = @"请输入用户名";
    _nameTF.textAlignment = NSTextAlignmentCenter;
    _nameTF.clearButtonMode = UITextFieldViewModeWhileEditing;
    [backView addSubview:_nameTF];
    [_nameTF mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(40);
        make.top.mas_equalTo(backView.mas_top).offset(60);
        make.left.mas_equalTo(backView.mas_left).offset(5);
        make.right.mas_equalTo(backView.mas_right).offset(-5);
    }];
    
    self.codeTF = [[UITextField alloc]init];
    _codeTF.delegate = self;
    _codeTF.borderStyle = UITextBorderStyleRoundedRect;
    _codeTF.placeholder = @"请输入密码";
    _codeTF.secureTextEntry = YES;
    _codeTF.textAlignment = NSTextAlignmentCenter;
    _codeTF.clearButtonMode = UITextFieldViewModeWhileEditing;
    [backView addSubview:_codeTF];
    [_codeTF mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.equalTo(_nameTF);
        make.left.equalTo(_nameTF);
        make.top.mas_equalTo(_nameTF.mas_bottom).offset(10);
    }];
    
    self.reCodeTF = [[UITextField alloc]init];
    _reCodeTF.delegate = self;
    _reCodeTF.borderStyle = UITextBorderStyleRoundedRect;
    _reCodeTF.placeholder = @"确认密码";
    _reCodeTF.secureTextEntry = YES;
    _reCodeTF.textAlignment = NSTextAlignmentCenter;
    _reCodeTF.clearButtonMode = UITextFieldViewModeWhileEditing;
    [backView addSubview:_reCodeTF];
    [_reCodeTF mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.equalTo(_nameTF);
        make.left.equalTo(_nameTF);
        make.top.mas_equalTo(_codeTF.mas_bottom).offset(10);
    }];
    
    self.phoneTF = [[UITextField alloc]init];
    _phoneTF.delegate = self;
    _phoneTF.borderStyle = UITextBorderStyleRoundedRect;
    _phoneTF.placeholder = @"请输入手机号";
    _phoneTF.secureTextEntry = YES;
    _phoneTF.textAlignment = NSTextAlignmentCenter;
    _phoneTF.clearButtonMode = UITextFieldViewModeWhileEditing;
    [backView addSubview:_phoneTF];
    [_phoneTF mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.equalTo(_nameTF);
        make.left.equalTo(_nameTF);
        make.top.mas_equalTo(_reCodeTF.mas_bottom).offset(10);
    }];
    
    self.nickButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [_nickButton setImage:[UIImage imageNamed:@"gou2"] forState:UIControlStateNormal];
    [_nickButton setImage:[UIImage imageNamed:@"gou1"] forState:UIControlStateSelected];
    [backView addSubview:_nickButton];
    [_nickButton addTarget:self action:@selector(btnAction:) forControlEvents:UIControlEventTouchUpInside];
    
    [_nickButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(16, 16));
        make.top.mas_equalTo(_phoneTF.mas_bottom).offset(5);
        make.left.mas_equalTo(_phoneTF.mas_left).offset(5);
    }];
    
    self.provisionBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    _provisionBtn.titleLabel.font = [UIFont systemFontOfSize:14];
    [_provisionBtn setTitle:@"同意《服务条款》" forState:UIControlStateNormal];
    [backView addSubview:_provisionBtn];
    [_provisionBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(120, 16));
        make.top.equalTo(_nickButton);
        make.left.mas_equalTo(_nickButton.mas_right).offset(5);
    }];
    
    self.backButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [_backButton setTitle:@"取消" forState:UIControlStateNormal];
    _backButton.backgroundColor = [UIColor colorWithRed:234 / 255.0 green:234 / 255.0 blue:234 / 255.0 alpha:1];
    [backView addSubview:_backButton];
    [_backButton addTarget:self action:@selector(backBtnAction:) forControlEvents:UIControlEventTouchUpInside];
    [_backButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(100, 40));
        make.left.mas_equalTo(_phoneTF.mas_left).offset(30);
        make.top.mas_equalTo(_provisionBtn.mas_bottom).offset(40);
        
    }];
    
    self.regButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [_regButton setTitle:@"注册" forState:UIControlStateNormal];
    _regButton.backgroundColor = [UIColor colorWithRed:39 / 255.0 green:100234 / 255.0 blue:236 / 255.0 alpha:1];
    [backView addSubview:_regButton];
    [_regButton addTarget:self action:@selector(regButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    [_regButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(100, 40));
        make.right.mas_equalTo(_phoneTF.mas_right).offset(-30);
        make.top.mas_equalTo(_provisionBtn.mas_bottom).offset(40);
    }];
}

- (void)btnAction:(UIButton *)btn
{
    btn.selected = !btn.selected;
}

- (void)backBtnAction:(UIButton *)btn
{
    [self dismissViewControllerAnimated:YES completion:^{
        
    }];
}
// 注册点击方法
- (void)regButtonAction:(UIButton *)btn
{
    WEAKSELF(weakSelf);
    if (![_nameTF.text isEqualToString:@""] && ![_codeTF.text isEqualToString:@""] && ![_reCodeTF.text isEqualToString:@""] && ![_phoneTF.text isEqualToString:@""]) {
        
        if (_nickButton.selected) {
            UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"注册成功，请登录" preferredStyle:UIAlertControllerStyleAlert];
            UIAlertAction *action = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                
                weakSelf.infoDic = [NSMutableDictionary dictionaryWithObjectsAndKeys:_nameTF.text, @"name",_codeTF.text, @"code", _phoneTF.text, @"phone", nil];
                weakSelf.reBack(_infoDic);
                [weakSelf dismissViewControllerAnimated:YES completion:^{
                    
                    
                }];
                
            }];
            [alert addAction:action];
            [weakSelf presentViewController:alert animated:YES completion:^{
            }];
            
        } else {
            UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"请阅读并同意服务条款" preferredStyle:UIAlertControllerStyleAlert];
            UIAlertAction *action = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                
            }];
            [alert addAction:action];
            [weakSelf presentViewController:alert animated:YES completion:^{
                
            }];
        }
    } else {
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"请完善个人信息" preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *action = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            
        }];
        [alert addAction:action];
        [weakSelf presentViewController:alert animated:YES completion:^{
            
        }];
    }
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];
    return YES;
}
// 结束编辑
- (void)textFieldDidEndEditing:(UITextField *)textField
{
    if (textField == _reCodeTF) {
        if (textField.text != _codeTF.text) {
            UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"两次输入密码不一致" preferredStyle:UIAlertControllerStyleAlert];
            UIAlertAction *action = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                
            }];
            [alert addAction:action];
            [self presentViewController:alert animated:YES completion:^{
                
            }];
        }
    }
}

- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    [_nameTF resignFirstResponder];
    [_codeTF resignFirstResponder];
    [_reCodeTF resignFirstResponder];
    [_phoneTF resignFirstResponder];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
